
*********** Generating proxy datasets

clear all
set seed 4444
cd 

*** (1) Customs

set obs 1000

gen cus_v_idfirm = _n

* Firm
gen cus_v_name = ""
forvalues n = 1/1000 {
	foreach a in A B C D E F G H I J K L M N O P Q R S T U V X Y Z {
		 di as red "`n'"
		 local b = runiform()
		 qui replace cus_v_name = cus_v_name + "`a'" if cus_v_idfirm == `n'&`b'<=0.5
		 qui replace cus_v_name = cus_v_name + lower("`a'") if cus_v_idfirm == `n'&`b'<=0.1		
		 local b = runiform()
		 qui replace cus_v_name = cus_v_name + " " if cus_v_idfirm == `n'&`b'<=0.1		
		 local b = runiform()
		 qui replace cus_v_name = cus_v_name + "." if cus_v_idfirm == `n'&`b'<=0.01			
		 qui replace cus_v_name = cus_v_name + "," if cus_v_idfirm == `n'&`b'>=0.99
	}
}

* Buyer
gen cus_v_buyer = ""
forvalues n = 1/1000 {
	foreach a in A B C D E F G H I J K L M N O P Q R S T U V X Y Z {
		 di as red "`n'"
		 local b = runiform()
		 qui replace cus_v_buyer = cus_v_buyer + "`a'" if cus_v_idfirm == `n'&`b'<=0.5
		 qui replace cus_v_buyer = cus_v_buyer + lower("`a'") if cus_v_idfirm == `n'&`b'<=0.1		
		 local b = runiform()
		 qui replace cus_v_buyer = cus_v_buyer + " " if cus_v_idfirm == `n'&`b'<=0.1		
		 local b = runiform()
		 qui replace cus_v_buyer = cus_v_buyer + "." if cus_v_idfirm == `n'&`b'<=0.01		
		 qui replace cus_v_buyer = cus_v_buyer + "," if cus_v_idfirm == `n'&`b'>=0.99	
	}
}

* Country
gen cc_GT = runiform()
gen cc_HN = runiform()
gen cc_PA = runiform()
gen cc_CR = runiform()
gen cc_NI = runiform()
gen cc_ES = runiform()
gen cc_US = runiform()
egen cc_max = rowmax(cc_*)
gen cus_v_cou = ""
foreach c in GT HN PA CR NI ES US {
	replace cus_v_cou = "`c'" if cc_max == cc_`c'	
}
drop cc_* 

* Product
gen pp_1 = runiform()
gen pp_2 = runiform()
gen pp_3 = runiform()
gen pp_4 = runiform()
gen pp_5 = runiform()
gen pp_6 = runiform()
gen pp_7 = runiform()
egen pp_max = rowmax(pp_*)
gen cus_v_prod = ""
foreach c in 1 2 3 4 5 6 7  {
	replace cus_v_prod = "`c'0000001" if pp_max == pp_`c'	
}
drop pp_*

* Transport mode
gen tt_TERRESTRE = runiform()
gen tt_MULTIMODAL = runiform()
gen tt_MARITIMO = runiform()
gen tt_AEREO = runiform()
egen tt_max = rowmax(tt_*)
gen cus_v_trans = ""
foreach c in TERRESTRE MULTIMODAL MARITIMO AEREO {
	replace cus_v_trans = "`c'" if tt_max == tt_`c'	
}
drop tt_*

* Custom
gen aa_5 = runiform()
gen aa_7 = runiform()
gen aa_8 = runiform()
gen aa_9 = runiform()
gen aa_11 = runiform()
egen aa_max = rowmax(aa_*)
gen cus_v_cust = ""
foreach c in 5 7 8 9 11 {
	replace cus_v_cust = "`c'" if aa_max == aa_`c'	
}
drop aa_*

* Date
gen cus_v_date = ""
replace cus_v_date = "15\07\2010" if cus_v_idfirm <=250
replace cus_v_date = "15\07\2011" if cus_v_idfirm <=500 & cus_v_idfirm >250
replace cus_v_date = "15\07\2012" if cus_v_idfirm <=750 & cus_v_idfirm >500
replace cus_v_date = "15\07\2013" if                      cus_v_idfirm >750

* Values
gen cus_v_val = exp(rnormal(10, 5))
qui sum cus_v_val
gen cus_v_wei = cus_v_val*exp(rnormal(0,5))

* Final data
gen cus_v_others = runiform()
save cus_database.dta, replace

*** (2) TIM

use cus_database.dta, clear

* Selection of treated firms
keep if cus_v_trans=="MULTIMODAL" | cus_v_trans=="TERRESTRE"
keep if (cus_v_date=="15\07\2011"&runiform()<0.1)| ///
	    (cus_v_date=="15\07\2012"&runiform()<0.3)| ///
		(cus_v_date=="15\07\2013"&runiform()<0.5)
		
sort cus_v_date
gen tim_v_dut = _n

* Renaming
rename cus_v_cust tim_vr_cust
rename cus_v_date tim_vd_date
rename cus_v_name tim_vd_name
rename cus_v_buyer tim_vd_buyer
rename cus_v_val tim_vp_val
rename cus_v_prod tim_vp_prod
rename cus_v_others tim_vd_others
drop cus_v_idfirm cus_v_trans cus_v_wei
		
* Country of data

gen tim_vr_cou = "El Salvador"

* Perturbing date
gen day = substr(tim_vd_date,1,2)
destring day, replace
replace day = day + round(3*(2*runiform()-1),1)	
tostring day, replace
replace tim_vd_date = day + substr(tim_vd_date,3,.)
drop day

* Internal custom

gen aa_1 = runiform()
gen aa_2 = runiform()
gen aa_3 = runiform()
egen aa_max = rowmax(aa_*)
gen tim_vr_cust_int = ""
foreach c in 1 2 3 {
	replace tim_vr_cust_int = "`c'" if aa_max == aa_`c'	
}
drop aa_*

* Exit country

rename cus_v_cou tim_vr_cou_end
replace tim_vr_cou_end = "PA" if tim_vr_cou_end == "ES"
replace tim_vr_cou_end = "GT" if tim_vr_cou_end == "US"

* Sub datasets

preserve 
keep tim_v_dut tim_vd_name tim_vd_buyer tim_vd_date tim_vd_others
order tim_v_dut
save tim_database_d.dta, replace
restore

preserve 
keep tim_v_dut tim_vp_prod tim_vp_val
order tim_v_dut
save tim_database_p.dta, replace
restore
 
keep tim_v_dut tim_vr_cou tim_vr_cust tim_vr_cust_int tim_vr_cou_end 
order tim_v_dut
save tim_database_r.dta, replace

*** (3) MUNICIP

use cus_database.dta, clear

gen mm_1 = runiform()
gen mm_2 = runiform()
gen mm_3 = runiform()
gen mm_4 = runiform()
gen mm_5 = runiform()
egen mm_max = rowmax(mm_*)
gen reg_v_mun = ""
foreach c in 1 2 3 4 5  {
	replace reg_v_mun = "0`c'01" if mm_max == mm_`c'	
}
drop mm_*

rename cus_v_idfirm reg_v_idfirm 
drop cus*

gen reg_v_others = runiform()

save reg_database.dta, replace



